package com.ezzd.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.ezzd.pojo.TbGoods;
import com.ezzd.service.GoodsService;
import com.ezzd.utils.GoodsReadListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @description tb_goods
 * @author 被遗忘的湫
 * @date 2022-08-01
 */
@Controller
@RestController
@RequestMapping(value = "/goods")
public class GoodsController {

    @Autowired //监听数据表上传excel并解析
    private GoodsReadListener goodsReadListener;
    @Resource
    private GoodsService goodsService;

    /**
     * 新增
     * @author 被遗忘的湫
     * @date 2022/08/01
     **/
    @RequestMapping("/insert")
    public Object insert(TbGoods tbGoods){
        return goodsService.insert(tbGoods);
    }

    /**
     * 刪除
     * @author 被遗忘的湫
     * @date 2022/08/01
     **/
    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable("id") int id){
        return goodsService.delete(id);
    }

    /**
     * 更新
     * @author 被遗忘的湫
     * @date 2022/08/01
     **/
    @RequestMapping("/update")
    public Object update(TbGoods tbGoods){
        return goodsService.update(tbGoods);
    }

    /**
     * 查询 根据主键 id 查询
     * @author 被遗忘的湫
     * @date 2022/08/01
     **/
    @RequestMapping("/load")
    public Object load(int id){
        return goodsService.load(id);
    }

    /**
     * 查询 分页查询
     * @author 被遗忘的湫
     * @date 2022/08/01
     **/
    @RequestMapping("/list")
    public Object pageList(@RequestParam(required = false, defaultValue = "1") int pageNum,
                           @RequestParam(required = false, defaultValue = "10") int pagesize) {
        return goodsService.pageList(pageNum, pagesize);
    }

    /**
     * 导出excel
     */
    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        List<TbGoods> goods = goodsService.queryAll();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 easyexcel没有关系
        String fileName = URLEncoder.encode("商品详情表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), TbGoods.class)
                //.registerWriteHandler(new ExcelWidthStyleStrategy())//自动列宽
                .sheet("商品").doWrite(goods);
    }

    /**
     * 上传文件 直接解析文件
     */
    @RequestMapping("/upload")
    public void upload(MultipartFile file, HttpServletResponse response) throws IOException {
        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), TbGoods.class, goodsReadListener);
        read.sheet().doRead();
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().write("<h1>上传成功！<h1>");
    }

}